<?php namespace Common\Api;

use Framework\Model\Db;

class User{

	public function get($id)
	{

		static $sCache = [];

		if(isset($sCache[$id]))
		{
			echo 333;
			return $sCache[$id];
		}

		$cache = S('user');
		if(empty($cache))
		{
			$cache = [];
		}

		if(isset($cache[$id]))
		{
			return $cache[$id];
		}else
		{
			$Db = new Db('stu');
			$user = $Db->where("id=$id")->first();
			$sCache[]=$user;


			//如果队列中的已经有这个用户,就将该用户放到缓存里
			if($user)
			{
				$cache[$id]=$user;
				//设置队列人数
				$size = 1000;
				while (count($cache)>$size) 
				{
					array_shift($cache);
				}
				S('user',$cache);
				return $user;
			}
		}
	}

	public function __call($method,$params)
	{
		if(substr($method,0,3)=='get')
		{
			$name = strtoupper(substr($method,3));

			if(empty($params))
			{
				if(isset($_SESSION['id']))
				{
					array_unshift($params,$_SESSION['id']);
				}
			}

			$user = call_user_func_array([$this,'get'],$params);
			if($user)
			{
				if(isset($user[$name]))
				{
					return $user[$name];
				}
			}
		}
	}
	


}